# -*- coding: utf-8 -*-
# -------------------------------------------------
# File Name： example_upload_file
# Description :
# Author : 'li'
# date： 2022/5/23
# -------------------------------------------------
# Change Activity:
# 2022/5/23:
# -------------------------------------------------
import uvicorn
from fastapi import FastAPI, File, UploadFile
from starlette.staticfiles import StaticFiles

from ml.micro_service.misc import get_swagger_static_file_dir

app = FastAPI()


@app.post("/files/")
async def create_file(file: bytes = File()):
    return {"file_size": len(file)}


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    filename = file.filename
    contents = await file.read()
    with open(filename, 'wb') as f:
        f.write(contents)
    return {"filename": file.filename}


def _main():
    swagger_dir = get_swagger_static_file_dir()
    app.mount('/static', StaticFiles(directory=swagger_dir), name='static')
    uvicorn.run(app, host="0.0.0.0", port=8000)


if __name__ == '__main__':
    _main()
